package com.wgy.day39.反射.Class对象功能;

import com.wgy.day39.domain.Person;

import java.lang.reflect.Constructor;

/**
 * Created by pc on 2020/12/15 21:51
 * Description: LearningJava
 */
/*
Class对象功能:之获取功能

 2.获取构造方法们(Constructor)
   Constructor<?>[] getConstructors():获取所有public修饰的构造方法
   Constructor<T>[] getConstructor(类<?>...parameterTypes):获取指定名称的public修饰的构造方法
   Constructor<T>[] getDeclaredConstructors():获取所有的构造方法,不考虑修饰符
   Constructor<T>[] getDeclaredConstructor(类<?>...parameterTypes):获取指定名称的构造方法,不考虑修饰符

获取到的Constructor后的操作:(构造方法)
                        创建对象:T newInstance(Object...initargs):


*/
public class Demo03Reflect_Constructor {
    public static void main(String[] args) throws Exception {
        //1.获取Person类的Class对象
        Class<Person> personClass = Person.class;

        //2.通过Class对象 获取构造器  Constructor<T>[] getConstructor(类<?>...parameterTypes):
        Constructor<Person> constructor = personClass.getConstructor(String.class, int.class);
        System.out.println(constructor);


        //3.创建对象
        Person p = constructor.newInstance("张三", 23);
        System.out.println(p);//Person{name='张三', age=23, a='null', b='null', c='null', d='null'}
        System.out.println("=========");


        //使用无参构造方法
        Constructor<Person> constructor1 = personClass.getConstructor();
        Person p2 = constructor1.newInstance();
        System.out.println(p2);//Person{name='null', age=0, a='null', b='null', c='null', d='null'}
        System.out.println("============");


        //使用Class类的方法newInstance来创建对象
        Person p3 = personClass.newInstance();
        System.out.println(p3);//Person{name='null', age=0, a='null', b='null', c='null', d='null'}


    }
}
